package com.example.utils;


import com.example.common.SpringApplicationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DBUtils {

    private static String URL;

    private static String USERNAME;

    private static String DRIVER;

    private static String PASSWORD;

    private static Connection connection;


    private static Logger logger = LoggerFactory.getLogger(DBUtils.class);



    public static Connection getConnection(){
        if(connection==null){
            initConnection();
        }
        return connection;
    }



    private static void initConnection() {
        try {
            Map<String, String> map= SpringApplicationContext.getBeanObj("map");
            String url=map.get("url");
            String driver=map.get("driver");
            String password=map.get("password");
            String username=map.get("username");
            Class.forName(driver);
            connection = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException var2) {
            var2.printStackTrace();
        } catch (SQLException var3) {
            var3.printStackTrace();
            closeConnection();
        }
    }



    private static String[] getFeildList(ResultSet rs) {
        try {
            ResultSetMetaData rsmd = rs.getMetaData();
            String[] lie = new String[rsmd.getColumnCount()];
            for(int i = 1; i < rsmd.getColumnCount() + 1; ++i) {
                lie[i - 1] = rsmd.getColumnName(i);
            }
            return lie;
        } catch (Exception var5) {
            var5.printStackTrace();
            return new String[0];
        }
    }


    private static ResultSet sqlExecuteQuery(String sql) {
        Statement stmt = null;
        ResultSet rs = null;
        try {
            if (connection == null) {
                initConnection();
            }
            stmt = connection.createStatement();
            rs = stmt.executeQuery(sql);
            return rs;
        } catch (Exception var5) {
            var5.printStackTrace();
        }
        return rs;
    }

    public static List<Map<String, Object>> selectMapList(String sql){
        return (List<Map<String, Object>>) selectObject(sql,true);
    }

    public static Map<String, Object> selectMapOne(String sql){
        return (Map<String, Object>) selectObject(sql,false);
    }

    private static Object selectObject(String sql,boolean isMore){
        logger.info("执行sql:==>" + sql);
        ResultSet resultSet = sqlExecuteQuery(sql);
        if(resultSet==null){
            return null;
        }
        List<Map<String, Object>> mapList=null;
        if(isMore){
            mapList=new ArrayList<>();
        }
        String[] fields = getFeildList(resultSet);
        try {
            while(resultSet.next()) {
                Map<String, Object> stringObjectMap = new HashMap();
                for(int i = 0; i < fields.length; ++i) {
                    String feildName = fields[i].toLowerCase();
                    Object value = resultSet.getObject(feildName);
                    stringObjectMap.put(feildName, value);
                }
                if(!isMore){
                    return stringObjectMap;
                }
                mapList.add(stringObjectMap);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return mapList;
    }

    public static boolean sqlExecute(String sql) {
        boolean flag = false;
        Statement stmt = null;
        try {
            if(connection==null){
                initConnection();
            }
            connection.createStatement();
            flag = stmt.execute(sql);
        } catch (Exception var8) {
            var8.printStackTrace();
        } finally {
            closeConnection();
        }
        return flag;
    }


    private static void closeConnection() {
        try {
            logger.info("连接关闭");
            connection.close();
        } catch (SQLException var2) {
            var2.printStackTrace();
        }
    }


}
